package com.yuanlrc.base.server.home;

import com.yuanlrc.base.bean.WebSocketMsg;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class WebSocketOfflineMessageStore {
    // 使用 ConcurrentHashMap 存储离线消息
    private static final Map<Long, List<WebSocketMsg>> offlineMessages = new ConcurrentHashMap<>();

    /**
     * 添加离线消息
     *
     * @param userId  用户ID
     * @param message 消息内容
     */
    public static void addOfflineMessage(Long userId, WebSocketMsg message) {
        offlineMessages.computeIfAbsent(userId, k -> new CopyOnWriteArrayList<>()).add(message);
    }

    /**
     * 获取用户的离线消息
     *
     * @param userId 用户ID
     * @return 离线消息列表
     */
    public static List<WebSocketMsg> getOfflineMessages(Long userId) {
        return offlineMessages.getOrDefault(userId, new CopyOnWriteArrayList<>());
    }

    /**
     * 清除用户的离线消息
     *
     * @param userId 用户ID
     */
    public static void clearOfflineMessages(Long userId) {
        offlineMessages.remove(userId);
    }
}
